﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;
using Resources;

namespace MonkeyWarranty.Web.Dealers
{
    public partial class Edit : System.Web.UI.Page
    {
        #region Properties

        private DealerBiz _dealerBiz;
        private DealerBiz DealerManager
        {
            get { return _dealerBiz ?? (_dealerBiz = new DealerBiz()); }
        }

        private int _dealerId;
        private int DealerId
        {
            get
            {
                if (_dealerId <= 0)
                    int.TryParse(Request.QueryString["Id"], out _dealerId);
                return _dealerId;
            }
        }

        private Dealer _dealer;
        private Dealer CurrentDealer
        {
            get
            {
                if (_dealer == null && DealerId > 0)
                {
                    _dealer = DealerManager.GetById(DealerId);
                }
                return _dealer;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load data
                // load dealer groups
                var groups = DealerManager.GetAllDealerGroups();//_repository.CityRepository.GetAllDealerGroups();
                groups.Insert(0, new DealerGroup { Id = 0 });
                ddlDealerGroups.DataTextField = "Name";
                ddlDealerGroups.DataValueField = "Id";
                ddlDealerGroups.DataSource = groups;
                ddlDealerGroups.DataBind();

                // Load dealer data
                if (CurrentDealer == null)
                {
                    lblErrorMessage.Text = string.Format("{0}: Id={1}", GlobalStrings.NotExisted, DealerId);
                    btnSave.Enabled = false;
                }
                else
                {
                    ddlDealerGroups.SelectedValue = CurrentDealer.DealerGroupId.ToString();
                    txtCode.Text = CurrentDealer.Code;
                    txtName.Text = CurrentDealer.Name;
                    txtAddress.Text = CurrentDealer.Address;
                    txtPhone.Text = CurrentDealer.Phone;
                    txtFax.Text = CurrentDealer.Fax;
                    txtEmail.Text = CurrentDealer.Email;
                    txtWebsite.Text = CurrentDealer.Website;
                    txtTaxCode.Text = CurrentDealer.TaxCode;
                    txtBankAccount.Text = CurrentDealer.BankAccount;
                    txtBankName.Text = CurrentDealer.BankName;
                    txtBankProvince.Text = CurrentDealer.BankProvince;
                    txtDirectorName.Text = CurrentDealer.DirectorName;
                    txtAccounterName.Text = CurrentDealer.AccounterName;
                    txtDescription.Text = CurrentDealer.Description;
                    txtSortOrder.Text = CurrentDealer.SortOrder.ToString(CultureInfo.InvariantCulture);
                    chkHidden.Checked = CurrentDealer.Hidden;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            errorMessage.Visible = false;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = GlobalStrings.CodeIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = GlobalStrings.NameIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;
            var address = txtAddress.Text.Trim();
            var phone = txtPhone.Text.Trim();
            var fax = txtFax.Text.Trim();
            var email = txtEmail.Text.Trim();
            var website = txtWebsite.Text.Trim();
            var taxcode = txtTaxCode.Text.Trim();
            var bankAccount = txtBankAccount.Text.Trim();
            var bankName = txtBankName.Text.Trim();
            var bankProvince = txtBankProvince.Text.Trim();
            var director = txtDirectorName.Text.Trim();
            var accounter = txtAccounterName.Text.Trim();
            int dealerGroupId;
            int.TryParse(ddlDealerGroups.SelectedValue, out dealerGroupId);

            // get data
            CurrentDealer.Code = code;
            CurrentDealer.Name = name;
            CurrentDealer.Address = address;
            CurrentDealer.Phone = phone;
            CurrentDealer.Fax = fax;
            CurrentDealer.Email = email;
            CurrentDealer.Website = website;
            CurrentDealer.TaxCode = taxcode;
            CurrentDealer.BankAccount = bankAccount;
            CurrentDealer.BankName = bankName;
            CurrentDealer.BankProvince = bankProvince;
            CurrentDealer.DirectorName = director;
            CurrentDealer.AccounterName = accounter;
            CurrentDealer.Description = descrition;
            CurrentDealer.SortOrder = sortOrder;
            CurrentDealer.Hidden = isHidden;
            CurrentDealer.DealerGroupId = dealerGroupId;

            // save data
            DealerManager.UpdateDealer(CurrentDealer);

            lblErrorMessage.Text = GlobalStrings.UpdateSuccess;
            errorMessage.Visible = true;
        }

        #endregion
    }
}